package controllers;

import java.util.Date;
import java.util.List;

import models.DrWords;

import org.apache.commons.lang.StringUtils;

import play.libs.Codec;
import services.CaseWordService;
import vos.ResultVo;
import vos.ReturnCode;

/**
 * 庭审词条
 * @author heweikang
 *
 */
public class CaseWordController extends BaseController {
	
	/**
	 * 新增词条
	 * @param context
	 * @param caseid
	 */
	public static void addCaseWord(DrWords drWord){
		if(drWord == null){
			renderRespondJson(ReturnCode.ErrorCode, "请输入常用词条");
		}
		if(StringUtils.isEmpty(drWord.getContext())){
			renderRespondJson(ReturnCode.ErrorCode, "请输入常用词条");
		}
		if(StringUtils.isEmpty(drWord.getCaseid())){
			renderRespondJson(ReturnCode.ErrorCode, "请先进行直播配置");
		}
		DrWords oldWord=CaseWordService.getByName(drWord.getContext());
		if(oldWord!=null){
			renderRespondJson(ReturnCode.ErrorCode, "该词条已存在");
		}
		if(drWord.getContext().contains("\n")||drWord.getContext().contains("\r")){
			drWord.setContext(drWord.getContext().replaceAll("\\n", " "));
			drWord.setContext(drWord.getContext().replaceAll("\\r", " "));
		}
		if(drWord.getContext().length()>100){
			renderRespondJson(ReturnCode.ErrorCode, "录入的内容超出长度限制");
		}
		drWord.setCasewordid(Codec.UUID());
		drWord.setCreateDate(new Date());
		String Ip=request.remoteAddress;
		boolean isSave = CaseWordService.addCaseWord(drWord,getLoginUser(),Ip);
		
		if(isSave){
			renderRespondJson(ReturnCode.SuccessCode,"保存成功");
		}
		renderRespondJson(ReturnCode.ErrorCode, "保存失败");
	}
	/**
	 * 根据案件编号获取常用词条
	 * @param caseid
	 */
	public static void findCaseWordByCaseId(String caseid){
		if(StringUtils.isEmpty(caseid)){
			renderRespondJson(ReturnCode.ErrorCode, "案件编号为空,请先进行直播配置");
		}
		
		List<DrWords> words = CaseWordService.findCaseWordByCaseId(caseid);
		
		
		ResultVo vo=new ResultVo(ReturnCode.SuccessCode,"成功",words);
		renderJSON(vo);
	}
	/**
	 * 根据编号删除案件常用词条
	 * @param caseWordid
	 */
	public static void deleteCaseWordById(String caseWordid){
		if(StringUtils.isEmpty(caseWordid)){
			renderRespondJson(ReturnCode.ErrorCode, "请选择要删除的优化记录");
		}
		
		String ip=request.remoteAddress;
		boolean isDel = CaseWordService.deleteCaseWordById(caseWordid,getLoginUser(),ip);
		if(isDel){
			renderRespondJson(ReturnCode.SuccessCode,"删除成功");
		}
		renderRespondJson(ReturnCode.SuccessCode,"删除失败");
	}
}
